/**
 * This class represents a single item in an rss feed.
 * @author Chirag Mistry 
 */

package ca.uwo.android.rss;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.text.Html;
import android.text.Spanned;
import android.util.Log;

public class RssItem implements Serializable{

	private static final long serialVersionUID = -8030510037208143535L;
	
	private String title;	//title of the feed item
	private String link;	//link of the feed item
	private String date;	//date of the feed item
	private Date dateObj;	//date object of the feed item
	private String description;	//description of the feed item
	private String location;
	private String image;
	
	public Date getDateObject(){
		return this.dateObj;
	}
	
	public String getDate(){
		return this.date;
	}

	public String getLink() {
		return this.link;
	}
	
	public String getTitle(){
		return this.title;
	}
	
	public String getDescription(){
		return this.description;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public void setDate(String date) {
		Log.d("RssItem", "Setting date to " + date);
		
		if (date.matches("\\d\\d\\d\\d\\x2D\\d{1,2}?\\x2D\\d{1,2}?")) {
			//Log.i("DATE: ", "date matches regex");
			
			//Log.d("RssItem- DATE:","inside set date of regex. Date is: "+date);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat eventFormat = new SimpleDateFormat("MMMM-dd-yy");
			try{
				
				this.dateObj = sdf.parse(date);
				Log.d("RssItem- DATE:","rssItem date parsed"+this.dateObj.toString());
				this.date = eventFormat.format(this.dateObj);
			}catch(java.text.ParseException e){
				//Log.e("RssItem", "Error parsing date " + date, e);
				e.printStackTrace();
			}
			
			//this.date = date;
		} else {
			this.date = date.substring(0, date.length() - 6);
		}
	}

	public void setDescription(String description) {
		/* ADD SPACES TO DESCRIPTIONS
		 * StringBuilder sb = new StringBuilder(description); int i; while((i =
		 * sb.indexOf("\n")) != -1){ Log.i("BLANK LINE: ", "found blank line");
		 * sb.replace(i, i+2, "<br><br>"); } this.description = sb.toString();
		 */
		this.description = description;
	}

	/**
	 * This method is used to create and return a copy 
	 * of the current rss item
	 * @return copy - copy of this rss item
	 */
	public RssItem copy() {
		RssItem copy = new RssItem();
		copy.title = this.title;
		copy.link = this.link;
		copy.date = this.date;
		copy.description = this.description;
		copy.location = this.location;
		copy.dateObj = this.dateObj;
		copy.image = this.image;
		
		return copy;
	}
	
	/**
	 * This method is used to print the contents of this rss item so that it 
	 * can be displayed in a module displaying an rss feed. The string contains 
	 * HTML formatting and when converted to spanned, it can be interpreted and 
	 * correctly displayed in a text view
	 * @return text - 	a spanned variable containing HTML code to specify the 
	 * 				appearance of the text
	 */
	public Spanned toNewsString() {
		
		String rssItem = "";
			
		rssItem += "<b><font color='#660099'>" + title + "</font></b>"
				+ "<br>" + "<i>" + date + "</i>" + "<br><br>" + description;
		
		Spanned text = Html.fromHtml(rssItem);	//convert string to spanned
		return text;
	}
	
	
	//toXML
	// <rssitem>
	// 		<title>TITLE</title>
	// 		<description>DESCRIPTION</description>
	// 		<date>DATE</date>
	// 		<link>LINK</link>
	// </rssitem>
	public String toXML() {
		StringBuffer sb = new StringBuffer();
		sb.append("<rssitem>\n");
		sb.append("\t<title>" + title + "</title>\n");
		sb.append("\t<description>" + description + "</description>\n");
		sb.append("\t<date>" + date + "</date>\n");
		sb.append("\t<link>" + link + "</link>\n");
		sb.append("\t<location>" + location + "</location>\n");
		sb.append("\t<image>" + image + "</image>\n");
		sb.append("</rssitem>\n");
		
		return sb.toString();
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("<rssitem>\n");
		sb.append("\t<title>" + title + "</title>\n");
		sb.append("\t<description>" + description + "</description>\n");
		sb.append("\t<date>" + date + "</date>\n");
		sb.append("\t<link>" + link + "</link>\n");
		sb.append("\t<location>" + location + "</location>\n");
		sb.append("\t<image>" + image + "</image>\n");
		sb.append("</rssitem>\n");
		
		return sb.toString();
	}
	public void setLocation(String location) {
		this.location = location;
	}

	public String getLocation() {
		return location;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getImage() {
		return image;
	}
}
